<template>
  <div class="app-container">
    <!-- 查询和其他操作 -->
    <div class="filter-container">
      <el-select v-model="listQuery.status" class="filter-item" placeholder="是否启用" title="是否启用"><el-option v-for="(item, index) in statusDic" :key="index" :label="item" :value="index" /></el-select>
      <el-button class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter">查找</el-button>
    </div>

    <el-table v-loading="listLoading" :data="list" element-loading-text="正在查询中。。。" border fit highlight-current-row>
      <el-table-column align="center" width="100px" label="编号" prop="id" sortable />

      <el-table-column align="center" label="vip名称" prop="levelName" sortable />

      <el-table-column align="center" label="vip价格" prop="levelPrice" />

      <el-table-column align="center" label="折扣" prop="discount" />

      <el-table-column align="center" label="直推费率" prop="direct" />

      <el-table-column align="center" label="间推费率" prop="indirect" />

      <el-table-column align="center" label="赠送货款" prop="presentation" />

      <el-table-column align="center" label="是否启用" prop="status" sortable>
        <template slot-scope="scope">
          <el-tag>{{ statusDic[scope.row.status] }}</el-tag>
        </template>
      </el-table-column>

      <el-table-column align="center" label="更新时间" prop="updateTime" />

      <el-table-column align="center" label="操作" width="250" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button type="primary" size="mini" @click="handleDetail(scope.row)">编辑</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit" @pagination="getList" />

    <!-- 用户编辑对话框 -->
    <el-dialog :visible.sync="userDialogVisible" title="收款信息">
      <el-form ref="userDetail" :model="userDetail" status-icon label-position="left" label-width="100px" style="width: 400px; margin-left:50px;">
        <el-form-item label="vip标识" prop="level">
          <el-input v-model="userDetail.level" :disabled="true" />
        </el-form-item>
        <el-form-item label="vip名称" prop="levelName">
          <el-input v-model="userDetail.levelName" />
        </el-form-item>
        <el-form-item label="vip价格" prop="levelPrice">
          <el-input v-model="userDetail.levelPrice" />
        </el-form-item>
        <el-form-item label="购物折扣" prop="discount">
          <el-input v-model="userDetail.discount" />
        </el-form-item>
        <el-form-item label="直推费率" prop="direct">
          <el-input v-model="userDetail.direct" />
        </el-form-item>
        <el-form-item label="间推费率" prop="indirect">
          <el-input v-model="userDetail.indirect" />
        </el-form-item>
        <el-form-item label="赠送货款" prop="presentation">
          <el-input v-model="userDetail.presentation" />
        </el-form-item>
        <el-form-item label="是否启用" prop="status">
          <el-select v-model="userDetail.status" placeholder="请选择">
            <el-option :value="1" label="启用" />
            <el-option :value="0" label="不启用" />
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="handleUserUpdate">确定已付款</el-button>
        <el-button @click="userDialogVisible = false">取消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { distributionList, updateDistribution } from '@/api/withdraw'
import Pagination from '@/components/Pagination'
export default {
  name: 'DistributionList',
  components: { Pagination },
  data() {
    return {
      list: null,
      total: 0,
      listLoading: true,
      listQuery: {
        page: 1,
        limit: 20,
        userId: undefined,
        username: undefined,
        sort: 'update_time',
        order: 'ASC'
      },
      downloadLoading: false,
      statusDic: ['否', '是'],
      userDialogVisible: false,
      userDetail: {}
    }
  },
  created() {
    this.getList()
  },
  methods: {
    getList: function() {
      this.listLoading = true
      distributionList(this.listQuery).then(response => {
        this.list = response.data.data.list
        this.total = response.data.data.total
        this.listLoading = false
      }).catch(() => {
        this.list = []
        this.total = 0
        this.listLoading = false
      })
    },
    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },
    handleUserUpdate() {
      this.userDetail.status = 1
      updateDistribution(this.userDetail)
        .then((response) => {
          this.userDialogVisible = false
          this.$notify.success({
            title: '成功',
            message: '更新用户成功'
          })
        })
        .catch(response => {
          this.$notify.error({
            title: '失败',
            message: response.data.errmsg
          })
        })
    },
    handleDetail(row) {
      this.userDetail = row
      this.userDialogVisible = true
    }
  }
}
</script>
